package com.qsmx.store.app.main.suit.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qsmx.store.app.R;
import com.qsmx.store.app.entity.suit.SuitDetailEntity;
import com.qsmx.store.app.main.suit.holder.SuitItemHolder;

import java.util.List;

public class SuitHomeAdapter extends RecyclerView.Adapter {

    private List<SuitDetailEntity.ContentBean> mSuitDetailList;
    private Context mContext;
    private int mLineNum;

    //定义接口
    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    //声明自定义的监听接口
    private SuitHomeAdapter.OnItemClickListener monItemClickListener = null;

    //提供set方法
    public void setonItemClickListener(SuitHomeAdapter.OnItemClickListener listener) {
        this.monItemClickListener = listener;
    }

    public SuitHomeAdapter(Context mContext, int lineNum) {
        this.mContext = mContext;
        this.mLineNum = lineNum;
    }

    public void setData(List<SuitDetailEntity.ContentBean> data) {
        this.mSuitDetailList = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View view = null;

        if (mLineNum >= 6 || mLineNum == 5) {
            view = LayoutInflater.from(mContext).inflate(R.layout.adapter_suit_6th, viewGroup, false);
            return new SuitItemHolder(view);
        } else if (mLineNum == 4 || mLineNum == 3) {
            view = LayoutInflater.from(mContext).inflate(R.layout.adapter_suit_4th, viewGroup, false);
            return new SuitItemHolder(view);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.adapter_suit_2th, viewGroup, false);
            return new SuitItemHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int position) {

        SuitDetailEntity.ContentBean data = mSuitDetailList.get(position);
        SuitItemHolder holder = (SuitItemHolder) viewHolder;

        holder.tvSuitCode.setText(data.getCode());
        holder.tvSuitName.setText(data.getName());
        holder.tvSuitDesc.setText(data.getDescription());
        holder.clContent.setBackgroundColor(Color.parseColor(data.getBackgroundColor()));
        holder.tvSuitCode.setTextColor(Color.parseColor(data.getForegroundColor()));
        holder.tvSuitName.setTextColor(Color.parseColor(data.getForegroundColor()));
        holder.tvSuitDesc.setTextColor(Color.parseColor(data.getForegroundColor()));

        holder.clContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                monItemClickListener.onItemClick(v, position);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (mSuitDetailList != null && mSuitDetailList.size() > 0) {
            return mSuitDetailList.size();
        } else {
            return 0;
        }
    }
}
